//
//  UIButton+Addition.m
//  分类总结
//
//  Created by 张令林 on 16/9/18.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "UIButton+Addition.h"

@implementation UIButton (Addition)

#pragma mark 创建一个带有文字和图片的按钮(左右排布)
+ (instancetype)buttonLRWithImgName:(NSString *)imagName title:(NSString *)title color:(UIColor*)color forSize:(CGFloat)size target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event
{
    UIButton *button = [[UIButton alloc] init];
    //添加图片
    [button setImage:[UIImage imageNamed:imagName] forState:UIControlStateNormal];
    //添加文字
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:size];
    //适应大小
    [button sizeToFit];
    //添加事件
    [button addTarget:target action:action forControlEvents:event];
    return button;
}

#pragma mark - 生成一个图片和按钮上下排布的按钮(上下排布,依赖于上面的NSAttributedString+Additon分类)
+ (instancetype)buttonTBImgName:(NSString *)imagName imageWH:(CGFloat)imageWH title:(NSString *)title forSize:(CGFloat)size color:(UIColor*)color spacing:(CGFloat)spacing target:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event
{
    NSAttributedString *attStr = [NSAttributedString imageTextWithImage:[UIImage imageNamed:imagName] imageWH:imageWH title:title fontSize:size titleColor:color spacing:spacing];
    UIButton* button = [[UIButton alloc] init];
    button.titleLabel.numberOfLines = 0;//一定记得这一步,否则可能不成功
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setAttributedTitle:attStr forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:target action:action forControlEvents:event];
    return button;
}




@end
